<?php

/**
 * 73YC 书城
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
// 判断调试模式是否开启缓存
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/*------------------------------------------------------ */
//-- Shopex系统地址转换
/*------------------------------------------------------ */
/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */
$cache_id = sprintf('%X', crc32($_SESSION['user_rank'] . '-' . $_CFG['lang']));
if (!$smarty->is_cached('bookstore.dwt', $cache_id))
{
    assign_template();
    $position = assign_ur_here();
    $smarty->assign('page_title',      $position['title']);    // 页面标题
    /* meta information */
    $smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords']));
    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc']));
    $smarty->assign('flash_theme',     $_CFG['flash_theme']);  // Flash轮播图片模板
    $smarty->assign('feed_url',        ($_CFG['rewrite'] == 1) ? 'feed.xml' : 'feed.php'); // RSS URL

    /* 获取所有书列表 */
    $goods_list = category_get_goods(get_children(601), "0", "0", "0", "", "10000", "1", "sort_order","DESC");
    
    /* 画种分类 */
    $cate_goods_list = array(
        array(1172,1178,1184,1186,1216,1222,1236,1185,),
        array(1195,1170,1199,1221,1224,1227,1228,1198,),
        array(1167,1169,1191,1223,1237,1194,1192,1168,),
        array(1215,1209,1238,1201,1171,1202,1230,1204,),
    );
    /* 新书上架 */
    $new_goods_list = array(1199,1216,1223,1233,1169,1226,1232,1177,1168,1198,1205,1229,1231,1212,1210,1193,);
    
    /* 系列丛书 */
    $series_goods_list = array(
        array(1180,1199,1200,1206,1210,1194,),
        array(1232,1231,1233,1236,1237,1239,),
        array(1212,1213,1215,1216,1217,1210,),
        array(1173,1169,1168,1175,1174,1177,),
        array(1221,1222,1223,1224,1225,1208,),
        );
    $smarty->assign('cate_goods_list',       id_get_goods($cate_goods_list,$goods_list));
    $smarty->assign('new_goods_list',        id_get_goods($new_goods_list,$goods_list));
    $smarty->assign('series_goods_list',     id_get_goods($series_goods_list,$goods_list));
    
    $ad_1=get_ad(17);//书城大广告
    $smarty->assign('ad_1',$ad_1);// 书城大广告
}
$smarty->assign('top_title', '美术书城');
$smarty->display('bookstore.dwt', $cache_id);

/*------------------------------------------------------ */
//-- PRIVATE FUNCTIONS
/*------------------------------------------------------ */

/**
 * 获得广告位
 *
 * @access  private
 * @return  array
 */
    
    function get_ad($position_id = 0){
        if(!$position_id) return false;
        $position_id = intval($position_id);
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('ad') . " WHERE position_id = '$position_id' ORDER BY ad_name ASC ";
        return $GLOBALS['db']->getAll($sql);
    }

/*产品列表*/
function category_get_goods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order)
{   
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.shop_price, g.goods_name,g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb, g.goods_img, g.is_hot ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            "WHERE $where $ext ORDER BY $sort $order,goods_id DESC";

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        $arr[$row['goods_id']]['goods_id']             = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']                 = $row['goods_name'];
        $arr[$row['goods_id']]['is_hot']               = $row['is_hot'];
        $arr[$row['goods_id']]['shop_price']           = $row['shop_price'];
        if($promote_price){
            $arr[$row['goods_id']]['promote_price']    = $row['promote_price'];
        }else{
            $arr[$row['goods_id']]['promote_price']    = '';
        }
        $arr[$row['goods_id']]['goods_thumb']          = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['url']                  = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['goods_img']           = $row['goods_img'];
    }

    return $arr;
}

/* 用ID获取商品列表 */
function id_get_goods($id_array,$goods_list){
    $data = array();
    if(is_array($id_array) && $id_array){
        foreach ($id_array as $key => $value) {
            if(is_array($value) && $value){
                foreach ($value as $k => $v) {
                    if($v){
                        $data[$key][$k] = $goods_list[$v];
                    }
                }
            }else{
                if($value){
                    $data[$key] = $goods_list[$value];
                }
            }
        }
    }
    return $data;
}
?>
